package com.longmao.service.impl;

import com.longmao.model.Pet;
import com.longmao.repository.PetRepository;
import com.longmao.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PetServiceImpl implements PetService {
    @Autowired
    private PetRepository petRepository;

    @Override
    public List<Pet> getAllPet() {
        return petRepository.findAll();
    }

    @Override
    public void addPet(Pet pet) {
        petRepository.save(pet);
    }

    @Override
    public void updatePet(Pet pet) {
        petRepository.saveAndFlush(pet);
    }

    @Override
    public void deletePet(int id) {
        petRepository.deleteById(id);
    }

    @Override
    public Pet getPetById(int id) {
        Optional<Pet> pet = petRepository.findById(id);
        if (pet.isPresent()) {
            return pet.get();
        } else {
            return null;
        }

    }
}
